import logging
import os
import ddddocr
from tools.page import page_get_no


def get_captcha(screenshot_path):
    ocr = ddddocr.DdddOcr()
    with open(screenshot_path, 'rb') as f:
        img_bytes = f.read()
    result = ocr.classification(img_bytes)
    return result

def login_ERP():
    page = page_get_no()
    page.get("http://192.168.16.240:8001/rebotlogin")
    logging.info('开始登录')
    page('@placeholder=请输入账号').input('zhanhuawei', clear=True)
    page('@placeholder=请输入密码').input('weiwei666', clear=True)
    page("t:img").save(r".\imgs", name='imgs.png', rename=False)
    result = get_captcha(r".\imgs\imgs.png")
    page('@placeholder=验证码').input(result)
    page(".el-button el-button--primary el-button--mini is-round").click()
    logging.info('登录成功')

def 上传ERP(file):
    login_ERP()
    page = page_get_no()
    page('text=客户服务').click()
    page('text=淘系投诉数据分析').click()
    page('text=导入').click()
    page('text=选取文件').click.to_upload(file)
    page.wait(5)
    page('text=上传').click()
    page.wait.ele_displayed('上传成功,正在导入中...', timeout=120)
    page.wait(1)
    logging.info('导入成功')


